//
//  HomeScene.cpp
//  ATM Killer
//
//  Created by Cloner Team on 10/5/14.
//
//

#include "HomeScene.h"
HomeScene::HomeScene() {
    this->gameModel = GameModel::getInstance();
    this->startGame = 0;
    this->createLayers();
    this->createLayersContent();
    this->scheduleUpdate();
}

HomeScene::~HomeScene() {
    
}

void HomeScene::onEnter() {
    AbstractScene::onEnter();
}

void HomeScene::onExit() {
    AbstractScene::onExit();
    this->removeAllChildrenWithCleanup(true);
}

void HomeScene::createLayers() {
    this->backgroundLayer = UGLayer::create();
    this->addChild(this->backgroundLayer);
    
    this->mainLayer = UGLayer::create();
    this->addChild(this->mainLayer);
}

void HomeScene::createLayersContent() {
    Size gameSize = this->gameModel->getDisplayResolutionSize();
    //background layer
    Face* background = new Face();
    background->initWithFile(BG_HOME);
    background->setPosition(gameSize.width/2, gameSize.height/2);
    this->backgroundLayer->addChild(background, 0);
    
    Face* foregroundLeft = new Face();
    foregroundLeft->initWithFile(BG_LEFT_HOME);
    foregroundLeft->setPosition(foregroundLeft->getContentSize().width/2 - 80, gameSize.height/2);
    this->backgroundLayer->addChild(foregroundLeft, 2);
    
    Face* foregroundRight = new Face();
    foregroundRight->initWithFile(BG_RIGHT_HOME);
    foregroundRight->setPosition(gameSize.width - foregroundRight->getContentSize().width/2 + 150, gameSize.height/2 - 70);
    this->backgroundLayer->addChild(foregroundRight, 1);
    
    Face* logo = new Face();
    logo->initWithSpriteFrameName((char*)ANI_LOGO);
    logo->setAnchorPoint(Point(1,0.5));
    logo->setPosition(gameSize.width - 10, gameSize.height * 550/640 + 10);
    this->backgroundLayer->addChild(logo, 4);
    
    //mainLayer
    UGMenu* buttonMenu = UGMenu::create();
    buttonMenu->setPosition(Point::ZERO);
    this->mainLayer->addChild(buttonMenu);
        //play button
    this->playButton = Utils::createButton((char*)ANI_BUTTON_GREEN, (char*)ANI_BUTTON_RED, (char*)"Start game", 35, (char*)FONT_TRANSFORMER, Color3B(255,255,255));
    this->playButton->setPosition(gameSize.width/2, 50);
    buttonMenu->addChild(this->playButton);
    this->playButton->setCallback(CC_CALLBACK_1(HomeScene::onButtonClick,this));
        //continue button
    this->continueButton = Utils::createButton((char*)ANI_BUTTON_RED, (char*)ANI_BUTTON_RED, (char*)"Continue game", 35, (char*)FONT_TRANSFORMER, Color3B(255,255,255));
    this->continueButton->setPosition(gameSize.width/2, 50);
    buttonMenu->addChild(this->continueButton);
    this->continueButton->setCallback(CC_CALLBACK_1(HomeScene::onButtonClick,this));
        //gamecenter button
    this->facebookButton = Utils::createButton((char*) ANI_BUTTON_FACEBOOK_1, (char*) ANI_BUTTON_FACEBOOK_2);
    this->facebookButton->setPosition(gameSize.width/2 - this->continueButton->getContentSize().width/2 - 10 - this->facebookButton->getContentSize().width/2, 50);
    this->facebookButton->setCallback(CC_CALLBACK_1(HomeScene::onButtonClick,this));

    buttonMenu->addChild(this->facebookButton);
        //rate button
    this->rateButton = Utils::createButton((char*) ANI_BUTTON_RATE_1, (char*)ANI_BUTTON_RATE_2);
    this->rateButton->setPosition(gameSize.width/2 + this->continueButton->getContentSize().width/2 + 10 + this->rateButton->getContentSize().width/2, 50);
    buttonMenu->addChild(this->rateButton);
    this->rateButton->setCallback(CC_CALLBACK_1(HomeScene::onButtonClick,this));
}

void HomeScene::onButtonClick(Ref* sender) {
    if ( (sender == this->continueButton || sender == this->playButton)&& this->startGame == 0) {
        this->notify(REQ_GAME_MODE, EVT_CLICK_BUTTON, NULL, 0, NULL);
        this->startGame = 1;
    }
    if  (sender == this->rateButton) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        AppUtils::openForRate();
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        Application* application = Application::getInstance();
        application->openURL(":callStore:");
#endif
    }
    if (sender == this->facebookButton) {
        if(sender == this->facebookButton) {
            char highScoreText[100];
            char* moneyText = this->gameModel->getCounter()->getTotalMoney()->display();
            sprintf(highScoreText,"I've get total $ %s. Am I the richest all over the world?Oh oh, beat me please!!!!!!", moneyText);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
            AppUtils::shareFacebook("Clicker Heroes", highScoreText ,"https://farm8.staticflickr.com/7493/15102766204_3de1b1e361_s.jpg", "https://itunes.apple.com/us/app/clicker-heroes/id926654548?ls=1&mt=8", "Sorry! Cannot connect to your facebook");
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
            char highScore[30];
            sprintf(highScore,":callFacebook:%s",moneyText);
            Application* application = Application::getInstance();
            application->getURL(highScore);
#endif
            delete[] moneyText;
        }

    }
}

void HomeScene::update(float dt) {
    SoundModel* soundModel = SoundModel::getInstance();
    soundModel->update(dt);
    if(this->gameModel->isPlayGame == true){
        this->continueButton->setVisible(true);
        this->continueButton->setEnabled(true);
        this->playButton->setEnabled(false);
        this->playButton->setVisible(false);
    } else {
        this->continueButton->setVisible(false);
        this->continueButton->setEnabled(false);
        this->playButton->setVisible(true);
        this->playButton->setEnabled(true);
    }
}
